#/bin/sh

base_path=$(cd `dirname $0`;pwd)
config_home=$base_path
kubernetes_home=$(cd `readlink -f $base_path/../kubernetes`;pwd)
flannel_home=$(cd `readlink -f $base_path/../flannel`;pwd)
etcd_home=$(cd `readlink -f $base_path/../etcd-v3.3.10-linux-amd64`;pwd)


MASTER_IP=10.2.238.250


echo "config prepath:"
echo $config_home
echo "kubernetes_home:"
echo $kubernetes_home
echo "flannel_home:"
echo $flannel_home
echo "etcd_home:"
echo $etcd_home

k8_bashrc="k8_environment.bashrc"
#sed -i '/K8_/d' $k8_bashrc
echo "#k8 environment " > $k8_bashrc 
echo "export K8_CONF_HOME=$config_home " >> $k8_bashrc
echo "export K8_KUBE_HOME=$kubernetes_home " >> $k8_bashrc
echo "export K8_FLANNEL_HOME=$flannel_home " >> $k8_bashrc
echo "export K8_ETCD_HOME=$etcd_home " >> $k8_bashrc
echo "export PATH=$kubernetes_home/kubernetes-server/server/bin:$kubernetes_home/kubernetes-node/node/bin:$kubernetes_home/kubernetes-client/client/bin:$flannel_home:$etcd_home:$PATH" >> $k8_bashrc
if [ ! -d "./etc" ]; then
    mkdir etc
    cp -r template_etc/* etc/
    for file in $(find etc/)
    do
        echo $file
        if [ -d $file ]; then
            continue
        fi
        eval sed -i 's,K8_CONF_HOME,'$config_home',g' $file
        sed -i "s,K8_KUBE_HOME,$kubernetes_home,g" $file
        sed -i "s,K8_FLANNEL_HOME,$flannel_home,g" $file
        sed -i "s,K8_ETCD_HOME,$etcd_home,g" $file
    done

fi

if [ ! -d "./etc/certificate" ]; then
    mkdir -p etc/certificate
    cd etc/certificate
    openssl genrsa -out ca.key 2048
    openssl req -x509 -new -nodes -key ca.key -subj "/CN=${MASTER_IP}" -days 365 -out ca.crt
    openssl genrsa -out server.key 2048
    openssl req -new -key server.key -subj "/CN=${MASTER_IP}" -out server.csr
    openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365
    cd $config_home
fi

if [ ! -d "./service" ]; then
    mkdir service
    cp template_service/* service/
    for file in $(ls service)
    do
        echo $file
        eval sed -i 's,K8_CONF_HOME,'$config_home',g' service/$file
        sed -i "s,K8_KUBE_HOME,$kubernetes_home,g" service/$file
        sed -i "s,K8_FLANNEL_HOME,$flannel_home,g" service/$file
        sed -i "s,K8_ETCD_HOME,$etcd_home,g" service/$file
    done

fi

service_home="/lib/systemd/system"


k8_master="k8_master.sh"

chmod +x $k8_master

echo "#k8 master run " > $k8_master

#copy service script to system folder

echo "#copy service script to system folder " >> $k8_master

echo "cp -f service/etcd.service $service_home/ " >> $k8_master
echo "cp -f service/kube-apiserver.service $service_home/ " >> $k8_master
echo "cp -f service/kube-controller-manager.service $service_home/ " >> $k8_master
echo "cp -f service/kube-scheduler.service $service_home/ " >> $k8_master

echo "#load service" >> $k8_master
echo "systemctl daemon-reload " >> $k8_master

#enable servivce

mkdir -p /var/lib/etcd

echo "#enable service" >> $k8_master
echo "systemctl enable etcd " >> $k8_master
echo "systemctl enable kube-apiserver " >> $k8_master
echo "systemctl enable kube-controller-manager " >> $k8_master
echo "systemctl enable kube-scheduler " >> $k8_master


#stop service
echo "#stop service " >> $k8_master
echo "systemctl stop etcd " >> $k8_master
echo "systemctl stop kube-apiserver " >> $k8_master
echo "systemctl stop kube-controller-manager " >> $k8_master
echo "systemctl stop kube-scheduler " >> $k8_master


#start service
echo "#start service " >> $k8_master
echo "systemctl start etcd " >> $k8_master
echo "systemctl start kube-apiserver " >> $k8_master
echo "systemctl start kube-controller-manager " >> $k8_master
echo "systemctl start kube-scheduler " >> $k8_master

echo "$etcd_home/etcdctl --endpoints=\"http://127.0.0.1:2379,http://127.0.0.1:4001,http://10.2.238.250:2379\"  \
    mk /coreos.com/network/config \
    '{\"Network\":\"192.168.0.0/16\",\"Backend\":{\"Type\": \"vxlan\"}}' " >> $k8_master



k8_node="k8_node.sh"

chmod +x $k8_node

echo "#K8 node run script " > $k8_node

# copy service script to system folder
echo "#copy service script to system folder " >> $k8_node
echo "cp -f service/flanneld.service $service_home/ " >> $k8_node
echo "cp -f service/kube-proxy.service $service_home/ " >> $k8_node
echo "cp -f service/kubelet.service $service_home/ " >> $k8_node

docker_service="$service_home/docker.service"
if [ -f "$docker_service" ]; then
    dockerServer=$(which dockerd)
    sed -i 's/flanneld.service //g' $docker_service
    sed -i 's/^After\=/After\=flanneld\.service /g' $docker_service
    sed -i '/flannel\/docker/d' $docker_service
    sed -i '/run\/flannel/d' $docker_service
    sed -i "s/^ExecStart/#ExecStart/g" $docker_service
    sed -i '/^#ExecStart/i\EnvironmentFile=\/run\/flannel\/docker' $docker_service
    sed -i "/var\/run\/docker/d" $docker_service
    sed -i '/^#ExecStart/a\ExecStart='$dockerServer' -H fd:// -H unix:///var/run/docker.sock \$DOCKER_OPTS' $docker_service
else
    echo "please install docker ,and enable docker.service,then rerun this script .or insert EnvironmentFile=-/run/flannel/docker for manual"
fi

echo "#load service" >> $k8_node
echo "systemctl daemon-reload " >> $k8_node


# eanble service


echo "mkdir -p /var/lib/kubelet" >> $k8_node

echo "#enable service" >> $k8_node
echo "systemctl enable flanneld " >> $k8_node
echo "systemctl enable kube-proxy " >> $k8_node
echo "systemctl enable kubelet " >> $k8_node


#stop service

echo "#stop service " >> $k8_node
echo "systemctl stop docker " >> $k8_node
echo "systemctl stop flanneld " >> $k8_node
echo "systemctl stop kube-proxy " >> $k8_node
echo "systemctl stop kubelet    " >> $k8_node

#关闭交换分区
#临时关闭
#echo "swapoff -a   " >> $k8_node


#尽量不使用交换分区，注意，在此不是禁用
#临时生效
#echo "sysctl -w vm.swappiness=0 " >> $k8_node
#永久生效
#echo "vm.swappiness = 0">> /etc/sysctl.conf 

#或者在kubelet启动参数中设置--fail-swap-on=false

#start service

echo "#start service " >> $k8_node
echo "systemctl start flanneld " >> $k8_node
echo "systemctl start kube-proxy " >> $k8_node
echo "systemctl start kubelet    " >> $k8_node

echo "systemctl start docker " >> $k8_node

ln -s "$config_home"/ext_bin/cfssl-certinfo_linux-amd64 /bin/cfssl-certinfo
ln -s "$config_home"/ext_bin/cfssljson_linux-amd64 /bin/cfssljson
ln -s "$config_home"/ext_bin/cfssl_linux-amd64 /bin/cfssl





